﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.IO;

namespace FileClient
{
    class ClientAPI
    {
        private Socket clientSock;
        private int bufferSize;



        public ClientAPI()
        {
            bufferSize = 1000;
        }

        public void start()
        {

            //Thread listen = new Thread(new ThreadStart(connections.senddata));
            //listen.Name = "Send Data";
            //listen.Start();
        }

        public void connect(String ip)
        {
            IPAddress[] ipAddress = Dns.GetHostAddresses(ip);
            IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], 5656);
            clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            clientSock.Connect(ipEnd);
        }

        public void sendfile(string filePath, string fileName)
        {
            try
            {
                string mess = "$FRANK,FILE,UPLOAD,putty.exe,the moon,test,";

                protocolmess(mess);

                byte[] buffer;
                FileStream neofiledata = new FileStream(filePath + fileName, FileMode.Open, FileAccess.Read);

                buffer = new byte[bufferSize];
                int count;

                while ((count = neofiledata.Read(buffer, 0, bufferSize)) > 0)
                {
                    System.Diagnostics.Debug.WriteLine("Bytes Sent: " + clientSock.Send(buffer, count, SocketFlags.None));
                }

                System.Diagnostics.Debug.WriteLine("file: {0} has been sent", fileName);
            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine(err);
            }
        }

        public void Login(string UserName, string Password)
        {
            string protocolPrefix = "$FRANK,LOGIN";
            string Message = protocolPrefix + "," + UserName + "," + Password + ",";
            protocolmess(Message);
            recMess(clientSock);

        }

        public void disconnect()
        {
            clientSock.Close();
        }



        private void protocolmess(string protocolMessage)
        {

            byte[] sendmess = Encoding.UTF8.GetBytes(protocolMessage);

            System.Diagnostics.Debug.WriteLine("Message sent: " + protocolMessage);

            clientSock.Send(sendmess);

        }
        private string recMess(Socket incomming)
        {
            byte[] buffer = new byte[1024];

            incomming.Receive(buffer);
            System.Diagnostics.Debug.WriteLine("Message Recived: " + Encoding.UTF8.GetString(buffer));
            return Encoding.UTF8.GetString(buffer);

        }

    }
}
/*#############SERVER PROTOCOL STRINGS++++++++++++++++
 * Start by selecting the Server
 * $FRANK
 * now Add what follows 
 */

/*##############FILE PROTOCOL STRINGS++++++++++++++++++++++
 *  Define what catergoy
 *  "FILE"
 * Define Function
 *      UPLOAD  Upload a file
 *      DOWNLOAD Download a File
 *EG
 *  To Send a File use the string 
 *  $FRANK,FILE,UPLOAD,<PARAMERTERS>,
 *  
 */


/*##############USER PROTOCOL STRINGS++++++++++++++++++++++
 *  Define what catergoy
 *  "USER"
 * Define Function
 *      CREATE  Create a User
 *      GET     Get User Data
 *      DELETE  DELETE A USER
 */